<template>
  <div>
    <h2>登录页面</h2>
    <table class="table table-bordered">
      <tr>
        <td>账号</td>
        <td>
          <input type="text" v-model="sdate.userName" />
        </td>
      </tr>
      <tr>
        <td>密码</td>
        <td>
          <input type="password" v-model="sdate.userPwd" />
        </td>
      </tr>
      <tr>
        <td></td>
        <td><input type="button" value="登录" @click="add" /></td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, onMounted } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
import { useUserStore } from '../Store/user';
const useStore = useUserStore();
const router = useRouter();
const route = useRoute();

let data = reactive({
  sdate: {
    userName: '',
    userPwd: ''
  }
});

let { sdate } = toRefs(data);

const add = () => {
  axios({
    url: 'http://localhost:58457/api/User/Login',
    method: 'get',
    params: {
      userName: sdate.value.userName,
      userPwd: sdate.value.userPwd
    }
  })
    .then((res) => {
      console.log(res);

      if (res.data.data.length == 0) {
        alert('登录失败牛逼');
        return;
      } else {
        zou(res.data.data[0].UId);
        console.log(res);
        // if (res.data.data == -1) {
        //   alert('登录失败');
        //   return;
        // }
      }
    })
    .catch((err) => {
      console.log(err);
    });
};

const zou = (UId: any) => {
  axios({
    url: 'http://localhost:58457/api/User/Panduan',
    method: 'get',
    params: {
      UId: UId
    }
  })
    .then((res) => {
      console.log(res);
      useStore.userInfo = res.data.data;
      if (res.data.data.UTId == 1) {
        alert('管理员登录');
        router.push('/adminShow');
      }
      if (res.data.data.UTId == 2) {
        alert('普通用户登录');
        router.push('/UserShow');
      }
    })
    .catch((err) => {
      console.log(err);
    });
};
</script>

<style scoped></style>
